from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
# from rest_framework.permissions import IsAuthenticated

from clinicalGuidelines.models import (
    ClinicalDepartmentModel,
    ClinicalGuidelinesModel,
    ClinicalGuidelineContentModel
)
from clinicalGuidelines.ser import (
    ClinicalDepartmentModelSerializer,
    ClinicalGuidelinesModelSerializer,
    ClinicalGuidelineContentModelSerializer
)

# 临床科室视图
class ClinicalDepartmentAPIView(APIView):
    # permission_classes = [IsAuthenticated]
    
    def get(self, request, format=None):
        departments = ClinicalDepartmentModel.objects.all().order_by('id')  # 添加排序
        serializer = ClinicalDepartmentModelSerializer(departments, many=True)
        return Response(serializer.data)
    
    def post(self, request, format=None):
        serializer = ClinicalDepartmentModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# 临床指南视图
class ClinicalGuidelinesAPIView(APIView):
    # permission_classes = [IsAuthenticated]
    
    def get(self, request, format=None):
        guidelines = ClinicalGuidelinesModel.objects.all().order_by('id')  # 添加排序
        serializer = ClinicalGuidelinesModelSerializer(guidelines, many=True)
        return Response(serializer.data)
    
    def post(self, request, format=None):
        serializer = ClinicalGuidelinesModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# 临床指南内容视图
class ClinicalGuidelineContentAPIView(APIView):
    # permission_classes = [IsAuthenticated]
    
    def get(self, request, format=None):
        guideline_contents = ClinicalGuidelineContentModel.objects.all().order_by('id')  # 添加排序
        serializer = ClinicalGuidelineContentModelSerializer(guideline_contents, many=True)
        return Response(serializer.data)
    
    def post(self, request, format=None):
        serializer = ClinicalGuidelineContentModelSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)